/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.util;

import com.comac.ins.common.core.exception.base.BaseException;
import jep.SharedInterpreter;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.*;

public class PythonExecute {
    private static final Logger log = LoggerFactory.getLogger(PythonExecute.class);
    private static final String fileName = "create_gltf_from_bdf_combined.py";
    static String scriptPath;

    static {
        try {
            scriptPath = saveFile(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public synchronized static Map excute(String execFunction, String data){

        try(SharedInterpreter jep = new SharedInterpreter()){

            log.info("python path:::"+scriptPath);
            jep.runScript(scriptPath);
            jep.eval("result = "+execFunction+"("+data+")");
            Map resultMap = jep.getValue("result", Map.class);
            jep.close();
            if (resultMap == null) {
                return null;
            }
            // 如果 Python 返回了错误信息
            if (resultMap.containsKey("error")) {
                log.error("解析错误: {}", resultMap.get("error"));
                throw new BaseException("解析错误: " + resultMap.get("error"));
            }
            return resultMap;
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException(e.getMessage());
        }
    }

    /**
     * 根据分辨率进行对齐
     *
     * @param resolution 分辨率 1920
     * @param values     数据集
     * @return 对齐后的数据，size=resolution
     */
    public static ArrayList<String> resolutionAlign(int resolution, List<String> values) {
        ArrayList<String> datas = new ArrayList<>();
        double i1 = 1.0 / values.size();
        double j1 = 1.0 / resolution;
        int j = 0;
        for (int i = 0; i < values.size(); i++) {
            String addNum = String.valueOf(values.get(i));
            if (j * j1 < (i + 1) * i1) {
                j += 1;
                datas.add(addNum);
            }
        }
        return datas;
    }


    public static String saveFile(String fileName) throws IOException {

        InputStream is = PythonExecute.class.getClassLoader().getResourceAsStream(fileName);
        String filePath = System.getProperty("user.dir");
        String path = filePath +File.separator+ fileName;
        File file = new File(path);
        FileUtils.copyInputStreamToFile(is, file);

        return path;
    }


    public static void main(String[] args) throws FileNotFoundException, ParseException {

//        System.setProperty("PYTHONHOME", "D:\\soft\\anaconda3");
//        System.setProperty("PYTHONPATH", "D:\\soft\\anaconda3\\Scripts");
//        SharedInterpreter jep = new SharedInterpreter();
        String path = System.getProperty("user.dir");
        System.out.println(path);
        System.out.println(ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "algorithm.py"));


//        ParamEntity entity = ParamEntity.builder().frequency("64").value(Arrays.asList("1.2","2.2","4.2",
//                        "3.3","2.2","4.2","9.2","5.3","6.2","9.5","6.8","2.7"
//                        ,"4.2","9.2","5.3","6.2","9.5","9.2","5.3","6.2"
//                        ,"2.2","4.2","9.2","5.3","6.2","9.5","8.5","7.3"))
//                .build();
        String data = "{\"value\":[\"1.2\",\"2.2\",\"4.2\",\"3.3\",\"2.2\",\"4.2\",\"9.2\",\"5.3\",\"6.2\",\"9.5\",\"6.8\",\"2.7\",\"4.2\",\"9.2\",\"5.3\",\"6.2\",\"9.5\",\"9.2\",\"5.3\",\"6.2\",\"2.2\",\"4.2\",\"9.2\",\"5.3\",\"6.2\",\"9.5\",\"8.5\",\"7.3\"],\"frequency\":\"64\"}\n";
        Map map = excute("specx",data);
        List<String> result = (List<String>) map.get("amp");
        System.out.println(result);

    }
}
